package com.xzc.jcip._3_.对象的共享.线程封闭.ThreadLocal;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 通过ThreadLocal为每个线程保存自己的变量
 * 从而简化参数传递
 */
public class Example1 {

    private static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>() {
        @lombok.SneakyThrows
        public Connection initialValue() {
            return DriverManager.getConnection("jdbc://xxx");
        }
    };

    //每个线程都有了属于自己的connection，而不用像短厅那样将JdbcDao传进去了，spring是否也有用到？
    public static Connection getConnection() {
        return connectionHolder.get();
    }
}
